package com.hall.controller;

import com.hall.pojo.Appointment;
import com.hall.pojo.AppointmentFiles;
import com.hall.pojo.SysUser;
import com.hall.pojo.common.Result;
import com.hall.service.CaseManagementService;
import com.hall.service.HallService;
import com.hall.service.UserService;
import com.hall.utils.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 病例建议添加页面
 */
@Controller
@RequestMapping("/case")
public class CaseManagementController {
    @Autowired
    private CaseManagementService caseManagementService;
    @Autowired
    private UserService userService;
    //添加病例描述/治疗建议
    @RequestMapping("/addCaseProposal")
    public String addCaseProposal(HttpSession session, HttpServletRequest request, @RequestParam("appointmentId") String appointmentId, Model model2) {
        SysUser s3 = UserSession.getSysUser(request);
        /*查询当前会诊id关联的所有病例文件以及病例数量,病例描述,专家建议*/
        List<Appointment> appointment5 = caseManagementService.findAppointmentById(appointmentId);
        System.out.println("结果为" + appointment5);
        /*查询当前会诊id的参会医院和参会者*/
        Appointment appointment6 = caseManagementService.findAppointUserById(appointmentId);
        model2.addAttribute("Login", userService.getUser( s3.getId()));
        model2.addAttribute("appointmentCase", appointment5) ; //提出来
        model2.addAttribute("appointmentUser", appointment6) ;
        return "case_treatment";
        //页面载入时吧所有关联的病例查出
    }

    @ResponseBody
    @GetMapping("/getCase/{id}")
    public Result<List<Appointment>> getCase(@PathVariable String id){
        if("".equals(id)||id==null)return Result.error("无效的id");
        List<Appointment> ap = caseManagementService.findAppointmentById(id);
        return  Result.success(ap,"请求成功");
    }

    //提交建议
    @RequestMapping("/updateCase")
    @ResponseBody
    public int updateCase(AppointmentFiles appointmentFiles) {
        if(!StringUtils.isEmpty(appointmentFiles)){
            int i = caseManagementService.updateCase(appointmentFiles);
            i=(i>0)?1:0;
            return i;
        }
        return 0;
    }
    @ResponseBody
    @GetMapping("/caseisnull/{appointmentId}")
    private int caseusnull(@PathVariable String appointmentId){
        /*查询当前会诊id关联的所有病例文件以及病例数量,病例描述,专家建议*/
        System.out.println("---------------------------"+appointmentId);
        List<Appointment> appointment8 = caseManagementService.findAppointmentById(appointmentId);
        System.out.println("--------------------------"+appointment8);
        if(appointment8.isEmpty()){
            return 0;
        }
        return 1;
    }
    //病例展示
    @RequestMapping("/caseShow")
    public String caseShow(@RequestParam("appointmentId") String appointmentId, Model model3){
        /*查询当前会诊id关联的所有病例文件以及病例数量,病例描述,专家建议*/
        List<Appointment> appointment8 = caseManagementService.findAppointmentById(appointmentId);
        /*查询当前会诊id的参会医院和参会者*/
        Appointment appointment7 = caseManagementService.findAppointUserById(appointmentId);
        model3.addAttribute("appointmentCase_2", appointment8);
        model3.addAttribute("appointmentUser_2", appointment7);
        return "case_show";
    }
}
